const fs = require('fs')
const path = require('path')
const binary = require('@mapbox/node-pre-gyp')

module.exports = function (content, map, meta) {
    const { context } = this
    const url = path.resolve(path.join(context, '../package.json'))

    try {
        fs.accessSync(url, fs.constants.R_OK)
        const binding_path = binary.find(url)

        const relativeUrl = path.relative(context, binding_path)
            .replace(/\\/g, '/')

        return `var binding = require('./${relativeUrl}');\nmodule.exports = exports = binding;`
    } catch (e) {
        console.error(e)
        throw e
    }
}
